package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 套餐分页查询
     *  使用多表联查的方式
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public Result pageSetMeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1、边界判断
        if(setmealPageQueryDTO == null){
            return Result.error("套餐查询失败...");
        }

        //2、开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //3、直接使用多表联查，获得套餐分页
        Page<SetmealVO> setmeals = setMealMapper.pageSetMeal(setmealPageQueryDTO);


        //4、封装
        PageResult pageResult = new PageResult(setmeals.getTotal() , setmeals.getResult());

        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public Result addSetMeal(SetmealDTO setmealDTO) {
        if(setmealDTO == null){
            return Result.error("新增套餐失败...");
        }

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.addSetMeal(setmeal);

        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }

        setmealDishMapper.addsetmealDishes(setmealDishes);

        return Result.success();
    }

    /**
     * 条件查询
     *
     * @param categoryId
     * @return
     */
    @Override
    public Result<List<Setmeal>> list(Long categoryId) {
        if(categoryId == null){
            return Result.error("查询菜套餐出错...");
        }

        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);

        List<Setmeal> list = setMealMapper.list(setmeal);
        return Result.success(list);
    }

    /**
     * 根据套餐id查询包含的菜品列表
     *
     * @param id
     * @return
     */
    @Override
    public Result<List<DishItemVO>> getDishItemById(Long id) {
        List<DishItemVO> list = setMealMapper.getDishItemBySetmealId(id);
        return Result.success(list);
    }

    /**
     * 根据IID查询套餐信息
     *
     * @param id
     * @return
     */
    @Override
    public Result getSetMealByID(Long id) {
        return null;
    }
}
